package org.jeecgframework.web.system.pojo.base;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.StaticValue;


/**
 * 
 * @author xiul
 * 2016-6-17 上午10:52:19
 * Description 检查完SN之后 保存数据
 */
public class ZyUpRoomThread extends Thread{
	
	private XSSFSheet sheet;
	private int begin;
	private int end;
	private List<ZyCinemaEntity> cinemaList;
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");

	public ZyUpRoomThread(){}
	
	/**
	 * @param beginow 开始行数
	 * @param endow 结束行数
	 * @param sheet sheet页
	 * @param cinemaList 
	 */
	public ZyUpRoomThread(int begin,int end,XSSFSheet sheet, List<ZyCinemaEntity> cinemaList){
		this.begin = begin;
		this.end = end;
		this.sheet = sheet;
		this.cinemaList = cinemaList;
	}
	
	public void run(){
		System.out.println("启动线程" + Thread.currentThread().getId());
		try {
			System.out.println("beginR:"+begin + ",endR:"+end);
			List<ZyRoomEntity> result = new ArrayList<ZyRoomEntity>();
			for (int r = begin; r <= end; r++) {
				XSSFRow row = sheet.getRow(r);
				ZyRoomEntity zyRoomEntity = new ZyRoomEntity();
				if(null != row){
					ZyCinemaEntity zyCinemaEntity = null;
					for (ZyCinemaEntity cinema : cinemaList) {
						if(cinema.getName() != null &&
								cinema.getName().equals(ExcelUtil.getCellValue(row.getCell(0)))){
							zyCinemaEntity = cinema;
							break;
						}
					}
					//ZyCinemaEntity zyCinemaEntity = systemService.findUniqueByProperty(ZyCinemaEntity.class, "name", ExcelUtil.getCellValue(row.getCell(0)));
					zyRoomEntity.setRoomCinema(zyCinemaEntity);
					zyRoomEntity.setRoomNum(ExcelUtil.getCellValue(row.getCell(1)));
					String seatNum = ExcelUtil.getCellValue(row.getCell(2));
					zyRoomEntity.setSeatNum(Integer.valueOf(StringUtil.isNotEmpty(seatNum) ? seatNum : "0"));
					String screenType = "";
					switch(ExcelUtil.getCellValue(row.getCell(3))){
					case "等高白幕" : 
						screenType = StaticValue.DIC_SCREEN_TYPE_HIGHWHITE;
						break;
					case "等宽白幕" : 
						screenType = StaticValue.DIC_SCREEN_TYPE_WIDTHWHITE;
						break;
					case "等宽金属幕" : 
						screenType = StaticValue.DIC_SCREEN_TYPE_WIDTHMETAL;
						break;
					case "等高金属幕" : 
						screenType = StaticValue.DIC_SCREEN_TYPE_HIGHMETAL;
						break;
					case "数字幕" : 
						screenType = StaticValue.DIC_SCREEN_TYPE_DIGITAL;
						break;
					default:
						screenType = "";
					}
					zyRoomEntity.setScreenType(screenType);
//					zyRoomEntity.setScreenPlus(String.valueOf(row.getCell(4).getNumericCellValue()));
//					zyRoomEntity.setScreenWidth(String.valueOf(row.getCell(5).getNumericCellValue()));
//					zyRoomEntity.setScreenHight(String.valueOf(row.getCell(6).getNumericCellValue()));
//					zyRoomEntity.setCurtainWidth(String.valueOf(row.getCell(7).getNumericCellValue()));
//					zyRoomEntity.setCurtainHight(String.valueOf(row.getCell(8).getNumericCellValue()));
//					zyRoomEntity.setShowKm(String.valueOf(row.getCell(9).getNumericCellValue()));
//					zyRoomEntity.setScreenLensRatio(String.valueOf(row.getCell(10).getNumericCellValue()));
//					zyRoomEntity.setCurtainLensRatio(String.valueOf(row.getCell(11).getNumericCellValue()));
					zyRoomEntity.setScreenPlus(ExcelUtil.getCellValue(row.getCell(4)));
					zyRoomEntity.setScreenWidth(ExcelUtil.getCellValue(row.getCell(5)));
					zyRoomEntity.setScreenHight(ExcelUtil.getCellValue(row.getCell(6)));	
					zyRoomEntity.setCurtainWidth(ExcelUtil.getCellValue(row.getCell(7)));
					zyRoomEntity.setCurtainHight(ExcelUtil.getCellValue(row.getCell(8)));
					zyRoomEntity.setShowKm(ExcelUtil.getCellValue(row.getCell(9)));
					zyRoomEntity.setScreenLensRatio(ExcelUtil.getCellValue(row.getCell(10)));
					zyRoomEntity.setCurtainLensRatio(ExcelUtil.getCellValue(row.getCell(11)));
					
					zyRoomEntity.setRoomSpecialCase(ExcelUtil.getCellValue(row.getCell(12)));
					zyRoomEntity.setIsNormal(ExcelUtil.getCellValue(row.getCell(13)).trim().toUpperCase().equals("Y")?"1":"0");
					zyRoomEntity.setExceptionReason(ExcelUtil.getCellValue(row.getCell(14)));
					zyRoomEntity.setIsDelete("0");
					zyRoomEntity.setCreateBy("");
					zyRoomEntity.setUpdateBy("");
					result.add(zyRoomEntity);
				}
			}
			systemService.batchSave(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.err.println(Thread.currentThread().getId()+"，执行完毕");
	}
	
	
	public int getBegin() {
		return begin;
	}
	public void setBegin(int begin) {
		this.begin = begin;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public XSSFSheet getSheet() {
		return sheet;
	}
	public void setSheet(XSSFSheet sheet) {
		this.sheet = sheet;
	}
	public List<ZyCinemaEntity> getCinemaList() {
		return cinemaList;
	}
	public void setCinemaList(List<ZyCinemaEntity> cinemaList) {
		this.cinemaList = cinemaList;
	}
	
	
	
}
